package Chapter2;

public class SortedSeqList<T extends Comparable<? super T>> extends SeqList<T> {
    public SortedSeqList(){
        super();
    }

    public SortedSeqList(int length){
        super(length);
    }

    public SortedSeqList(T[] values){
        super(values.length);
        for (int i=0;i<values.length;i++)
            this.insert(values[i]);
    }

    /**
     * 排序线性表的数据元素只有可读性，不支持设置操作，故将其覆盖
     * @param i
     * @param x
     */
    public void set(int i,T x){
        throw new java.lang.UnsupportedOperationException("set(int i,T x)");
    }

    /**
     * 不支持在指定位置插入
     * @param i
     * @param x
     * @return
     */
    public int insert(int i,T x){
        throw new java.lang.UnsupportedOperationException("insert(int i,T x)");
    }

    public int insert(T x){
        int i=0;
        if (this.isEmpty()||x.compareTo(this.get(this.n-1))>0)
            i=this.n;
        else
            while (i<this.n&&x.compareTo(this.get(i))>=0)
                i++;
        super.insert(i,x);
        return i;
    }

    public int search(T key){
        System.out.println(this.getClass().getName()+".search("+key+"),");
        for (int i=0;i<this.n&&key.compareTo(this.get(i))>=0;i++){
            if (key.compareTo(this.get(i))==0)
                return i;
        }
        return -1;
    }
}
